package com.cnswhy.cloud.server.app.controller.wallet;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.app.controller.BaseController;
import com.cnswhy.cloud.server.common.entity.enumeration.BusType;
import com.cnswhy.cloud.server.common.entity.enumeration.TransType;
import com.cnswhy.cloud.server.common.entity.enumeration.WalletType;
import com.cnswhy.cloud.server.core.channel.hcpay.service.HcpayService;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerWallet;
import com.cnswhy.cloud.server.core.entity.SplitTransRecord;
import com.cnswhy.cloud.server.core.entity.WalletTransDtl;
import com.cnswhy.cloud.server.core.oem.SystemService;
import com.cnswhy.cloud.server.core.query.QueryGenerator;
import com.cnswhy.cloud.server.core.service.*;
import com.cnswhy.cloud.server.core.service.cps.CpsChannelInfoService;
import com.cnswhy.cloud.server.core.service.customer.CustomerBankService;
import com.cnswhy.cloud.server.core.vo.RespVo;
import com.cnswhy.cloud.server.core.wallet.WalletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j

@Api(tags = "钱包")
@Controller
@RequestMapping("/wallet")
public class WalletController extends BaseController {

    @Autowired
    private RedisService redisService;
    @Autowired
    IdGeneratorService idGeneratorService;
    @Autowired
    CustomerBankService customerBankService;
    @Autowired
    SystemService systemService;
    @Autowired
    CpsChannelInfoService cpsChannelInfoService;
    @Autowired
    CpsBankRateService cpsBankRateService;
    @Autowired
    CpsTransOrderService cpsTransOrderService;
    @Autowired
    HcpayService hcpayService;
    @Autowired
    MessageService messageService;
    @Autowired
    WalletService walletService;
    @Autowired
    WalletTransDtlService walletTransDtlService;
    @Autowired
    CustomerWalletService customerWalletService;
    @Autowired
    SplitTransRecordService splitTransRecordService;
    @ApiOperation(value = "批量内转", httpMethod = "POST")
    @ApiImplicitParams({
    })
    @PostMapping("/batchTransfer")
    @ResponseBody
    public RespVo batchTransfer(HttpServletRequest request) {
        RespVo respVo = new RespVo();
        try {
            Customer loginCustomer = getLoginCustomer( request);
            List<CustomerWallet> customerWalletList = customerWalletService.list(Wraps.<CustomerWallet>lbQ().eq(CustomerWallet::getCustomerId,loginCustomer.getId()));
            for(CustomerWallet customerWallet : customerWalletList){
                if(WalletType.BALANCE.eq(customerWallet.getWalletType())){
                    continue;
                }
                if(customerWallet.getAvlbBalance().compareTo(Long.valueOf(0)) > 0){
                    walletService.transfer(customerWallet.getCustomerId(),customerWallet.getWalletType(), TransType.INNER_TRANSFER, customerWallet.getAvlbBalance() );

                }
            }
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("成功");
        } catch (Exception e) {
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("异常");
        }
        return respVo;
    }
    @ApiOperation(value = "交易记录查询", httpMethod = "POST")
    @ApiImplicitParams({
    })
    @PostMapping("/ajaxTransDtlData")
    @ResponseBody
    public RespVo ajaxTransDtlData(HttpServletRequest request,@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, WalletType walletType) {
        RespVo respVo = new RespVo();
        try {
            Customer loginCustomer = getLoginCustomer( request);
            CustomerWallet customerWallet = walletService.getCustomerWallet(loginCustomer.getId(),walletType);
            WalletTransDtl walletTransDtl = new WalletTransDtl();
            walletTransDtl.setCustomerWalletId(customerWallet.getId());
            QueryWrapper<WalletTransDtl> queryWrapper = QueryGenerator.initQueryWrapper(walletTransDtl);
            queryWrapper.orderByDesc("create_time");
            Page<WalletTransDtl> page = new Page<>(currentPage, pageSize);
            IPage<WalletTransDtl> pageList = walletTransDtlService.page(page, queryWrapper);
            respVo.setResult(pageList);
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("成功");
        } catch (Exception e) {
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("异常");
        }
        return respVo;
    }
    @ApiOperation(value = "收益记录", httpMethod = "POST")
    @ApiImplicitParams({
    })
    @PostMapping("/ajaxSplitRecordData")
    @ResponseBody
    public RespVo ajaxSplitRecordData(HttpServletRequest request, @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, BusType busType) {
        RespVo respVo = new RespVo();
        try {
            Customer loginCustomer = getLoginCustomer( request);
            SplitTransRecord splitTransRecord = new SplitTransRecord();
            splitTransRecord.setSplitCustomerId(loginCustomer.getId());
            splitTransRecord.setBusType(busType);
            QueryWrapper<SplitTransRecord> queryWrapper = QueryGenerator.initQueryWrapper(splitTransRecord);
            queryWrapper.orderByDesc("create_time");
            Page<SplitTransRecord> page = new Page<>(currentPage, pageSize);
            IPage<SplitTransRecord> pageList = splitTransRecordService.page(page, queryWrapper);
            respVo.setResult(pageList);
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("成功");
        } catch (Exception e) {
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("异常");
        }
        return respVo;
    }
}
